package com.gxa.modules.payment.service.impl;

import com.gxa.modules.payment.entity.MeterCycle;
import com.gxa.modules.payment.form.MeterCycleForm;
import com.gxa.modules.payment.form.MeterCycleQueryForm;
import com.gxa.modules.payment.mapper.MeterCycleMapper;
import com.gxa.modules.payment.mapper.MeterMapper;
import com.gxa.modules.payment.service.MeterCycleService;
import com.gxa.modules.payment.util.DocNoGenerationUtil;
import com.gxa.modules.payment.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MeterCycleServiceImpl implements MeterCycleService {

    @Autowired
    private MeterCycleMapper meterCycleMapper;

    @Autowired
    private MeterMapper meterMapper;

    @Autowired
    private DocNoGenerationUtil docNoGenerationUtil;

    @Override
    public List<MeterCycle> queryByPage(Map<String, Object> params) {
        List<MeterCycle> meterCycles = this.meterCycleMapper.queryByPage(params);
        return meterCycles;
    }

//    @Override
//    public Integer addNo() {
//        Integer integer = this.docNoGenerationUtil.generateDocNo(3);
//        return integer;
//    }

    @Override
    public String queryByCellName(Integer cycleId) {
        String cellName = this.meterCycleMapper.queryByCellName(cycleId);
        return cellName;
    }


    @Override
    public void meterCycleAdd(MeterCycleForm meterCycleForm) {
        Integer cycleId = this.docNoGenerationUtil.generateDocNo(3);

        Integer meterCode = meterCycleForm.getMeterCode();
        Integer integer = this.meterMapper.queryItemId(meterCode);
        meterCycleForm.setCycleId(cycleId);

        Date createTime = new Date();
        meterCycleForm.setCreateTime(createTime);
        meterCycleForm.setItemId(integer);

        this.meterCycleMapper.addMeterCycle(meterCycleForm);
    }

    @Override
    public Result<List<MeterCycleQueryForm> > queryMeterDetails(Integer cycleId,Integer page,Integer limit) {
        List<MeterCycleQueryForm> meterCycleQueryForms = this.meterCycleMapper.queryMeterDetails(cycleId,(page-1)*limit,limit);
        Integer integer = this.meterCycleMapper.queryCount(cycleId);

        Result<List<MeterCycleQueryForm> > result = new Result<>();
        result.setTotal(integer).setData(meterCycleQueryForms);
        return result;
    }

    @Override
    public void meterCycleDelete(Integer id) {
        this.meterCycleMapper.deleteMeterCycle(id);
    }

    @Override
    public void meterCycleDeleteIds(List<Integer> ids) {
        this.meterCycleMapper.deleteMeterCycleIds(ids);
    }
}
